package com.jd.product.service.impl;

import com.jd.basic.utils.BaiduAuditUtils;
import com.jd.basic.utils.LoginContext;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.product.domain.Product;
import com.jd.product.domain.ProductOnlineAuditLog;
import com.jd.product.domain.ProductDetail;
import com.jd.product.mapper.ProductDetailMapper;
import com.jd.product.mapper.ProductMapper;
import com.jd.product.mapper.ProductOnlineAuditLogMapper;
import com.jd.product.service.IProductService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.user.domain.Logininfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jd
 * @since 2023-03-18
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper mapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper auditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    @Transactional
    public void add(Product product) {
        // 在新增服务的时候我们需要创建时间
        product.setCreatetime(new Date());
        product.setState(0);
        super.add(product);
        // 保存详情表
        ProductDetail detail = new ProductDetail();
        detail.setProductId(product.getId());
        detail.setIntro(product.getDetail().getIntro());
        detail.setOrderNotice(product.getDetail().getOrderNotice());
        mapper.add(detail);
    }

    @Override
    public void update(Product product) {
        // 修改服务表
        super.update(product);
        // 修改详情表
        ProductDetail detail = mapper.findByProductId(product.getId());
        detail.setIntro(product.getDetail().getIntro());
        detail.setOrderNotice(product.getDetail().getOrderNotice());
        mapper.update(detail);
    }


    // 服务下架
    @Override
    public void offSale(List<Long> ids) {
        // 将服务的状态修改为下架
        // 添加服务下架时间
        productMapper.batchUpdateById(ids);

    }
    // 服务上架
    @Override
    public void onSale(List<Long> ids) {
        // 校验参数
        // 先判断该服务是否处于下架状态(在sql语句中直接判断，state=0）
        List<Product> products =productMapper.findByIds(ids);
        // 新建productIdList批量修改上架状态
        List<Long> productIdList = new ArrayList<>();
        //
        List<ProductOnlineAuditLog> productAuditLogs = new ArrayList<>();
        // 非空判断
        if (null == products){
            return;
        }
        // 百度AI校验
        products.forEach(product -> {
            // 新建一个字符串 用来拼接百度校验结果
            StringBuffer stringBuffer = new StringBuffer();
            // 先判断该服务是否处于下架状态
            // 先检验名称
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                stringBuffer.append("服务名称不合规，服务id为：").append(product.getId());
            }
            // 在校验图片
            String resources = product.getResources();
            if (StringUtils.isNotEmpty(resources)){
                String[] resource = resources.split(",");
                for (int i = 0; i < resource.length; i++) {
                    if (!BaiduAuditUtils.ImgCensor(resource[i])){
                        stringBuffer.append("服务图片不符合规范，服务Id为：").append(product.getId()).append("图片为：")
                                .append(resource[i]);
                    }
                }
            }
            // 添加服务审核日志(审核不通过添加)
            if (stringBuffer.length()>0){
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditTime(new Date());
                Logininfo loginInfo = LoginContext.getLoginInfo();
                Employee employee = employeeMapper.findByLogininfoId(loginInfo.getId());
                // 当前管理员id
                log.setAuditId(employee.getId());
                log.setNote(stringBuffer.toString());
                log.setProductId(product.getId());
                log.setState(0);
//                mapper.add(log);
                productAuditLogs.add(log);
                return;
            }
            productIdList.add(product.getId());
        });
        // 批量修改状态
        if (!CollectionUtils.isEmpty(productIdList)){
            productMapper.batchUpdate(productIdList);
        }
        // 批量添加日志
        if (!CollectionUtils.isEmpty(productAuditLogs)){
            auditLogMapper.batchAdd(productAuditLogs);
        }

    }

    @Override
    public void delete(Long id) {
        super.delete(id);
        // 删除product同时删除product——detail
        mapper.deleteByProductId(id);
    }
}
